﻿/**
 * 分阶段加载本地数据缓存
 */
app.loading.set(0, () => cache.load());
app.loaded.set(0, () => {
    let userInfo = localCache.global.get('userInfo');
    if (userInfo && userInfo.user && userInfo.user.name) {
        $(".header .user-info a.nav-link:first").html('[ ' + userInfo.user.name + ' ]');
        var $menu = $('.header .user-menu');
        if ($.com.isArray(userInfo.resources)) {
            for (var i = 0; i < userInfo.resources.length; i++) {
                let item = userInfo.resources[i];
                $menu.append(`<li class="nav-item"><a class="nav-link text-dark" href="${item.content}">${item.title}</a></li>`);
            }
        }
    }
});

/**
 * 本地数据缓存
 */
var cache = new (class _Cache_ {
    constructor() {
        this.parameters = [];
    }

    set(data) {
        this.parameters = data;
        this.root = this.parameters.get({ key: 'root' }, null, true);
        this.top = this.parameters.get(x => x.parent && x.parent.key === 'root');
        this.dataState = this.parameters.get(x => x.parent && x.parent.key === 'dataState');
        this.authorizeType = this.parameters.get(x => x.parent && x.parent.key === 'authorizeType');
        this.assignType = this.parameters.get(x => x.parent && x.parent.key === 'assignType');
        this.identityType = this.parameters.get(x => x.parent && x.parent.key === 'identityType');
        this.resourceType = this.parameters.get(x => x.parent && x.parent.key === 'resourceType');
        this.allowType = this.parameters.get(x => x.parent && x.parent.key === 'allowType');
        this.accessLevel = this.parameters.get(x => x.parent && x.parent.key === 'accessLevel');
        this.authType = this.parameters.get(x => x.parent && x.parent.key === 'authType');
        this.yesOrNo = this.parameters.get(x => x.parent && x.parent.key === 'yesOrNo');
        this.haveOrNot = this.parameters.get(x => x.parent && x.parent.key === 'haveOrNot');
        this.agreeOrNot = this.parameters.get(x => x.parent && x.parent.key === 'agreeOrNot');
    }

    load() {
        return Promise.resolve()
            .then(() => {
                let userInfo = localCache.global.get('userInfo');
                if (!userInfo) {
                    return axios.get('/account/getUserInfo')
                        .then(resp => resp.data)
                        .then(data => localCache.global.set('userInfo', data));
                }
            })
            .then(() => {
                let parameters = localCache.global.get('parameters');
                if ($.com.isArray(parameters)) {
                    this.set(parameters);
                } else {
                    return axios.get('/odata/parameters?$select=id,index,key,value,text,parentid&$expand=parent($select=id,key)&$orderby=index')
                        .then(resp => resp.data)
                        .then(data => {
                            data = $.com.dataAdapter(data);
                            if ($.com.isArray(data)) {
                                localCache.global.set('parameters', data)
                                this.set(data);
                            }
                        });
                }
            });
    }
})();